#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Copyright © 启明星辰 版权所有
# @Time    : 2023/4/18 13:38
# @Author  : 胡浩浩
# @Email   : hu_haohao@venusgroup.com.cn
# @File    : yaml_tools.py
# @IDE     : PyCharm
# @description :
import ruamel.yaml
import yaml


def write_to_yml(file_path, data):
    # 顺序写入新的yml文件中,保证格式不混乱
    with open(file_path, "w", encoding="utf-8") as f:
        ruamel.yaml.round_trip_dump(data, f, default_flow_style=False, allow_unicode=True)


def read_yml(file_path):
    with open(file_path, "r", encoding="utf-8") as f:
        data = ruamel.yaml.YAML(typ='unsafe', pure=True).load(f)
    return data


def append_write_to_yml(file_path, data):
    # 顺序写入新的yml文件中,保证格式不混乱
    with open(file_path, "a", encoding="utf-8") as f:
        yaml.dump(data=data, stream=f, allow_unicode=True)


if __name__ == '__main__':
    append_write_to_yml("../extract_global_variable.yml", {"aa1": "胡浩浩"})
